|
1
|
|
|
import {ForeignKey} from "../Model"; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
export default class Index { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
constructor() { |
|
12
|
|
|
this.indexes = new Map(); |
|
13
|
|
|
this.indexedFields = new Set(); |
|
14
|
|
|
this.splittedIndexNames = new Map(); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
* @param {Model} model |
|
20
|
|
|
* @param {string} indexName |
|
21
|
|
|
*/ |
|
22
|
|
|
static registerIndex(model, indexName) { |
|
23
|
|
|
globalThis.Store.database().registerIndex(model.constructor.className(), indexName); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @param {Model} model |
|
29
|
|
|
* @param {ForeignKey} foreignKeyField |
|
30
|
|
|
*/ |
|
31
|
|
|
static addIndex(model, foreignKeyField) { |
|
32
|
|
|
globalThis.Store.database().addIndex(model.className, foreignKeyField.foreignKey, foreignKeyField.fieldValue, model.primaryKey); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @param {Model} model |
|
38
|
|
|
* @param {ForeignKey} foreignKeyField |
|
39
|
|
|
*/ |
|
40
|
|
|
static removeIndex(model, foreignKeyField) { |
|
41
|
|
|
globalThis.Store.database().removeIndex(model.className, foreignKeyField.foreignKey, foreignKeyField.previousValue, model.primaryKey); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* @param {Model} model |
|
47
|
|
|
*/ |
|
48
|
|
|
static removeTmpIdFromIndex(model) { |
|
49
|
|
|
let className = model.className; |
|
50
|
|
|
model.dirtyFields.filter(field => field instanceof ForeignKey).forEach((field) => { |
|
51
|
|
|
globalThis.Store.database().removeIndex(className, field.$name, field.originalValue, model._tmpId); |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* @param key |
|
58
|
|
|
* @return {any} |
|
59
|
|
|
*/ |
|
60
|
|
|
getIndexByKey(key) { |
|
61
|
|
|
return this.indexes.get(key); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
* @param model |
|
67
|
|
|
* @param name |
|
68
|
|
|
*/ |
|
69
|
|
|
registerIndex(indexName) { |
|
70
|
|
|
if (!this.indexes.has(indexName)) { |
|
71
|
|
|
this.indexedFields.add(indexName); |
|
72
|
|
|
this.splittedIndexNames.set(indexName, indexName.split('.')); |
|
73
|
|
|
this.indexes.set(indexName, new Map()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
* @param indexName |
|
80
|
|
|
* @param lookUpKey |
|
81
|
|
|
* @param id |
|
82
|
|
|
*/ |
|
83
|
|
|
addIndex(indexName, lookUpKey, id) { |
|
84
|
|
|
if (!this.indexes.has(indexName) || id === null) { |
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
let current = this.indexes?.get(indexName); |
|
89
|
|
|
if (!(current.has(lookUpKey))) { |
|
90
|
|
|
this.registerLookUpKey(indexName, lookUpKey, id); |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
current = current?.get(lookUpKey); |
|
94
|
|
|
|
|
95
|
|
|
if (current.has(id)) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
current.add(id); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* |
|
104
|
|
|
* @param {string} indexName |
|
105
|
|
|
* @param lookUpKey |
|
106
|
|
|
* @param id |
|
107
|
|
|
*/ |
|
108
|
|
|
registerLookUpKey(indexName, lookUpKey, id) { |
|
109
|
|
|
this.indexes?.get(indexName)?.set(lookUpKey, new Set([id])); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* |
|
114
|
|
|
* @param {string} indexName |
|
115
|
|
|
* @param lookUpKey |
|
116
|
|
|
*/ |
|
117
|
|
|
unregisterLookUpKey(indexName, lookUpKey) { |
|
118
|
|
|
this.indexes?.get(indexName)?.delete(lookUpKey); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* |
|
123
|
|
|
* @param {Model} model |
|
124
|
|
|
* @param {string} fieldName |
|
125
|
|
|
* @return {*|null} |
|
126
|
|
|
*/ |
|
127
|
|
|
getIndexLookUpValue(model, fieldName) { |
|
128
|
|
|
const lookUpValue = this.splittedIndexNames.get(fieldName); |
|
129
|
|
|
let indexLookUpValue = model; |
|
130
|
|
|
for (const lookUpField of lookUpValue) { |
|
131
|
|
|
if (indexLookUpValue[lookUpField] === null) { |
|
132
|
|
|
break; |
|
133
|
|
|
} |
|
134
|
|
|
indexLookUpValue = indexLookUpValue[lookUpField]; |
|
135
|
|
|
} |
|
136
|
|
|
return indexLookUpValue ?? null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* |
|
141
|
|
|
* @param {Model} model |
|
142
|
|
|
*/ |
|
143
|
|
|
addValueToIndexes(model) { |
|
144
|
|
|
for (let [fieldName, value] of this.indexes) { |
|
145
|
|
|
let indexLookUpValue = this.getIndexLookUpValue(model, fieldName); |
|
146
|
|
|
if (indexLookUpValue === null) { |
|
147
|
|
|
continue; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
let current = value; |
|
151
|
|
|
if (!(current.get(indexLookUpValue) instanceof Set)) { |
|
152
|
|
|
current.set(indexLookUpValue, new Set()); |
|
153
|
|
|
} |
|
154
|
|
|
current.get(indexLookUpValue)?.add(model.primaryKey); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* |
|
160
|
|
|
* @param {Model} model |
|
161
|
|
|
*/ |
|
162
|
|
|
removeValueFromIndex(model) { |
|
163
|
|
|
for (let [fieldName, value] of this.indexes) { |
|
164
|
|
|
let indexLookUpValue = this.getIndexLookUpValue(model, fieldName); |
|
165
|
|
|
if (!indexLookUpValue || !value.has(indexLookUpValue)) { |
|
166
|
|
|
continue; |
|
167
|
|
|
} |
|
168
|
|
|
value?.get(indexLookUpValue)?.delete(model.primaryKey); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* |
|
174
|
|
|
*/ |
|
175
|
|
|
truncate() { |
|
176
|
|
|
for (let key in this.indexes) { |
|
177
|
|
|
this.indexes.get(key).clear(); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* |
|
183
|
|
|
* @param {String} indexName |
|
184
|
|
|
* @param lookUpKey |
|
185
|
|
|
* @param id |
|
186
|
|
|
*/ |
|
187
|
|
|
removeIndex(indexName, lookUpKey, id) { |
|
188
|
|
|
this.indexes?.get(indexName)?.get(lookUpKey)?.delete(id); |
|
189
|
|
|
} |
|
190
|
|
|
} |